#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

static pthread_mutex_t mutex;
static pthread_cond_t cond;
static int flag = 0;

void *mythread(void *param)
{
  while(1)
  {
    if(flag == 1)
    {
      pthread_mutex_lock(&mutex);
      printf("mythread is cond_wait\n");
      pthread_cond_wait(&cond,&mutex);  
      flag = 0;
      pthread_mutex_unlock(&mutex);
    }
    printf("mythread is run\n");
  }


}


int main(void)
{
  pthread_t tid;
  pthread_mutex_init(&mutex,NULL);
  pthread_cond_init(&cond,NULL);
  pthread_create(&tid,NULL,mythread,NULL);
  while(1) 
  {

    pthread_mutex_lock(&mutex);
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
    printf("mythread is signal\n");
    sleep(1);
    flag = 1;//阻塞
    printf("mythread is sleep\n");
    sleep(1);
  }
  pthread_mutex_destroy(&mutex);
  pthread_cond_destroy(&cond);
  pthread_cancel(tid);

  pthread_join(tid, NULL);
  return 0;
}
